home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #41 (Feb 89) / vblTask code / vblTask.c < prev    next >
C/C++ Source or Header  |  1988-10-14  |  5KB  |  157 lines

  1. /************************************************************************************************
  2. *    vblTask.c                                                                                      *
  3. *              an VBL task animation example                                                    *
  4. *                                                                                                *
  5. *         Written by Dick Chandler                                                                *
  6. *                                                                                                  *
  7. *             This program is intentionally keep as simple as possible, so that it could be         *
  8. *            easily entered and/or understood.  Therefore there are no calls made to check        *
  9. *            events nor any menus implemented.                                                    *
  10. *                                                                                                  *
  11. *************************************************************************************************/
  12.  
  13.  
  14. /*****  the only includes we'll need  *****/
  15. #include    <types.h>
  16. #include    <quickdraw.h>
  17. #include    <windows.h>
  18. #include    <toolutils.h>
  19. #include    <events.h>
  20. #include    <Retrace.h>
  21. #include    <OSUtils.h>
  22. #include    <Strings.h>
  23.  
  24.  
  25. /*****  we need to define our own 'A5' routines  *****/
  26. pascal void SetUpA5a() extern 0x2f0d;
  27. pascal void SetUpA5b() extern 0x2a78;
  28. pascal void SetUpA5c() extern 0x0904;
  29.  
  30. #define    SetUpA5() SetUpA5a(); SetUpA5b(), SetUpA5c()
  31.  
  32. pascal void RestoreA5() extern 0x2a5f;
  33.  
  34. /* just for readibilty        */
  35. #define        NOT        !
  36.  
  37.  
  38. /*****    Globals  *****/
  39. Rect        iconRec =        {   30, 30, 62, 62         };        /* location of pause icon            */
  40. Rect        boundsRect =    {   30, 30, 120, 120     };        /* window rectangle                    */
  41. VBLTask        StatusTask;                                        /* tasks VBL record                    */
  42. void        vblTask(), installTask(), removeTask();            /* task routines                    */
  43. long        interval;                                        /* time between icons                */
  44.  
  45. /*****    constants  *****/
  46. #define        FIRST_ICON        1000
  47. #define        LAST_ICON        1005
  48. #define        MAX_NUMBER_ICONS    (LAST_ICON - FIRST_ICON) +1
  49. #define        SECONDS                60
  50.  
  51.  
  52. /************************************************************************************************
  53. *                                    the main program                                                                            *
  54. *************************************************************************************************/
  55. int main()
  56. {
  57. WindowRecord    editWindowRecord;                        
  58. WindowPtr        editWindowPtr;
  59.  
  60.     /*
  61.     *    the init's we need  
  62.     */
  63.     InitGraf        (&qd.thePort);                        /* init quickdraw                        */
  64.     InitWindows        ();                                    /* init the window manager                */
  65.     
  66.     /*
  67.     *    open a small, simple (and humble) window  
  68.     */
  69.     editWindowPtr     = NewWindow(&editWindowRecord,  &boundsRect, "", 
  70.                     true, 1, (-1), false,  nil);
  71.     SetPort            ( editWindowPtr );
  72.  
  73.     /*
  74.     *    set task icons in motion  
  75.     */
  76.     interval         = SECONDS/8;                        /* set the interval between icons        */
  77.     installTask        ( &StatusTask );                    /* install our task on vbl queue        */
  78.     
  79.     /*
  80.     *    run until the button is clicked 
  81.     */
  82.     while    ( NOT Button() )                                     
  83.         ;
  84.     
  85.     /*
  86.     *    clean up and exit
  87.     */
  88.     removeTask        ();                                    /* remove our task on vbl queue            */
  89.     CloseWindow        (editWindowPtr);                    /* flush window memory                    */
  90.     return 0;                                            /* MPW Return                             */
  91.  
  92. }
  93.  
  94. /************************************************************************************************
  95. *    NAME:        installTask
  96. *
  97. *    INPUT:        StatusTaskPtr  -  a pointer to a VBL task record
  98. *
  99. *    FUNCTION:    installs our vblTask after seting the vbl parameters        
  100. *
  101. *************************************************************************************************/
  102. void installTask( StatusTaskPtr )
  103. VBLTask        *StatusTaskPtr;                            /* tasks VBL record                        */
  104. {
  105.     StatusTaskPtr->vblAddr         = vblTask;            /* set to out task function            */
  106.     StatusTaskPtr->vblCount     = 10;                /* set initial time to 10 ticks            */
  107.     StatusTaskPtr->vblPhase     = 0;                /* no offset needed                        */
  108.     StatusTaskPtr->qType         = vType;            /* type VBL task                        */
  109.     VInstall    (StatusTaskPtr);                    /* now install the task                    */
  110. }    
  111.  
  112. /************************************************************************************************
  113. *    NAME:        removeTask
  114. *
  115. *    INPUT:        StatusTask  -  a pointer to a VBL task record
  116. *
  117. *    FUNCTION:    removes our vbl task    
  118. *
  119. *************************************************************************************************/
  120. void removeTask( StatusTaskPtr )
  121. VBLTask        *StatusTaskPtr;                            /* tasks VBL record                        */
  122. {
  123.     VRemove    (StatusTaskPtr);                            /* remove task from VBL                    */
  124. }    
  125.  
  126.  
  127. /************************************************************************************************
  128. *    NAME:        vblTask                                                    
  129. *
  130. *    FUNCTIONS:    our actual vbl task which gets called every 'interval' ticks
  131. *
  132. *************************************************************************************************/
  133. void vblTask()
  134. {
  135. static long            icons;                                
  136. Handle                tempIcon;                            /* handle to icon                         */
  137. static long            timeIcon = FIRST_ICON;                
  138.  
  139.     SetUpA5();                                            /* set up A5 register                    */
  140.  
  141.     icons                 = MAX_NUMBER_ICONS;
  142.     StatusTask.vblCount = interval;                        /* reset task to correct time            */
  143.  
  144.     timeIcon++;                                            /* increment icon                         */
  145.     if    ( timeIcon > LAST_ICON )                        
  146.         timeIcon     = FIRST_ICON;
  147.  
  148.     tempIcon     = GetIcon( timeIcon );                        
  149.     if    ( tempIcon )
  150.         PlotIcon( &iconRec, tempIcon );
  151.     
  152.     RestoreA5();                                        /* preserve A5 register                    */
  153. }
  154.  
  155.  
  156.  
  157.